﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NewBusiness;
using NewBusiness.Domain;
using NewBusiness.Domain.Queries;
using NewBusiness.Domain.Uow;
using NewBusiness.Domain.Uow.Data;
using NewBusiness.Domain.Uow.Repositories;

namespace TMS.NewBusiness.Domain
{
    public interface IUserContext
    {
        IUserQueries UserQueries { get; }
        UserProfile GetUser(string Name);
        void CreateUser(UserProfile user);
    }

    public class UserContext : DomainContext, IUserContext
    {
        public IUserQueries UserQueries { get; private set; }
        public UserContext(IUow uow, ISession session, IUserQueries userQueries, MongoDB.Driver.MongoDatabase mdb)
            : base(uow, session, mdb)
        {
            this.UserQueries = userQueries;
        }


        public UserProfile GetUser(string Name)
        {
            return (from u in this.Uow.Users.GetAll()
                where u.UserName.ToLower().Equals(Name.ToLower())
                select u).FirstOrDefault();
        }

        public void CreateUser(UserProfile user)
        {
            this.Uow.Users.Create(user);
            this.Uow.Commit();
        }
    }
}
